class AlbumsController < ApplicationController
  
before_filter :login_required

def new    
  @album = Album.new  
end
  
def create    
  @album = Album.new params[:album] 
     if @album.title==''
        @album.destroy
        flash[:notice] = "Type the name, please"
        redirect_to :controller=>"albums",  :action=>"new"  and return false
     end
  begin
    FileUtils.mkdir(RAILS_ROOT+ '/public/photos/'+ @album.title + '/')
  end
  
  @album.user_id = session[:user]    
  if request.post? and @album.save 
    flash[:notice] = "\"#{@album.title}\"" + _(" has been created")      
    redirect_to :controller=>"admin", :action=>"album_list"       
  end
  
  rescue Errno::EEXIST
    flash[:notice] = "Album exists"   
    @album.destroy
    redirect_to :controller=>"albums",  :action=>"new"  and return false
  rescue Errno::EINVAL
    flash[:notice] = "Bad name"   
    @album.destroy
    redirect_to :controller=>"albums",  :action=>"new"  and return false
end


def edit
    
@album = Album.find(params[:id])
  
end

def update
  @album = Album.find(params[:id])
  k=@album.title
      
  if request.post? and @album.update_attributes(params[:album]) and @album.save
    begin
      File.rename(RAILS_ROOT+ '/public/photos/'+ k, RAILS_ROOT+ '/public/photos/'+ @album.title)
    end
    flash[:notice] = "\"#{@album.title}\"" + _(" has been saved.")
    redirect_to :controller=>"admin", :action=>"album_page", :id => @album
  else
    render :action=>"edit"
  end
  rescue Errno::EEXIST
    if (k == @album.title) 
      redirect_to :controller=>"admin", :action=>"album_list" and return false
    end
    @album.title = k
    @album.save
    flash[:notice] = "Album exist"      
    render :action=>"edit"
  rescue Errno::EINVAL
    @album.title = k
    @album.save
    flash[:notice] = "Bad name"      
    render :action=>"edit"
end

def add_photo
  @photo = Photo.find(params[:id].split("_")[1])
  @album = Album.find(params[:album_id])
  @album.photos << @photo if !@album.photos.include?(@photo)
  render :partial=>"/gallery/album" and return
end
  
  # Removes all the photos from the album, but keeps the album
  def clear
    @album = Album.find(params[:id])
    @album.photos.clear
    flash[:notice] = "\"#{@album.title}\"" + _(" has been cleared.")
    redirect_to :controller=>"admin", :action=>"album_page", :id => @album
  end
  
  # Removes one photo from an album
  def remove
    album = Album.find(params[:album_id])
    photo = Photo.find(params[:id].split("_")[1])
    album.photos.delete(photo)
    album.save
    render :partial=>"gallery/photo", :collection=>album.photos
  end
  
  # Removes all the photos from the album, but keeps the album
  def destroy
    @album = Album.find(params[:id])
    title = @album.title
    @i = 0
    @photos = @album.photos
    while @i < @photos.size do 
     @photos[@i].rm(title)
     @i+=1 
    end
    FileUtils.remove_dir 'public/photos/'+@album.title + '/' 
    @album.photos.clear
    title = @album.title
    @album.destroy
    flash[:notice] = "\"#{title}\"" + _(" has been destroyed.")
    redirect_to :controller=>"admin", :action=>"album_list"
  end

end
